#include "MainWindow.h"

MainWindow::MainWindow(User *user)
{
    this->user = user;

    widget = new QWidget;

    layout_main = new QHBoxLayout;
    layout_menu = new QVBoxLayout;
    layout_func = new QVBoxLayout;
    layout_search = new QHBoxLayout;
    layout_table = new QVBoxLayout;
    layout_column = new QHBoxLayout;

    button_list = new QVector<QPushButton*>;
    label_list = new QVector<QLabel*>;
    button_help = new QPushButton;

    input_search = new QLineEdit();
    but_search = new QPushButton();
    list_books = new QListWidget();

    list_id = new QVector<QString>;
    list_name = new QVector<QString>;
    list_author = new QVector<QString>;
    list_state = new QVector<QString>;
}

MainWindow::~MainWindow()
{
    delete list_id;
    delete list_name;
    delete list_author;
    delete list_state;

    delete button_help;
    for(int i = 0; i < list_books->count(); i++)
    {
        delete list_books->item(i);
    }
    delete list_books;
    delete but_search;
    delete input_search;

    for (QVector<QLabel*>::const_iterator iter = label_list->begin();iter != label_list->end(); iter++)
    {
        delete *iter;
    }
    delete label_list;

    for (QVector<QPushButton*>::const_iterator iter = button_list->begin();iter != button_list->end(); iter++)
    {
        delete *iter;
    }
    delete button_list;

    delete layout_column;
    delete layout_table;
    delete layout_search;
    delete layout_func;
    delete layout_menu;
    delete layout_main;

    delete widget;
}

void MainWindow::init_view_main()
{
    widget->setFixedSize(800, 600);
    widget->setWindowTitle("图书管理系统");

    layout_main->setDirection(QBoxLayout::LeftToRight);//设置排列方式
    layout_main->setSpacing(10);    //设置控件距离
    widget->setLayout(layout_main);

    layout_menu->setDirection(QBoxLayout::TopToBottom); //设置排列方式
    layout_menu->setSpacing(10);    //设置控件距离
    layout_main->addLayout(layout_menu);
    init_view_menu();

    layout_func->setDirection(QBoxLayout::BottomToTop); //设置排列方式
    layout_func->setSpacing(10);    //设置控件距离
    layout_main->addLayout(layout_func);
    init_view_func();
}

void MainWindow::init_view_menu()
{

    QString user_label[] = {"用户", "订阅", "导出"};
    for(int i = 0; i < 3; i++)
    {
        QPushButton *button = new QPushButton;
        button->setText(user_label[i]);
        button->setStyleSheet("QLabel{background:#dddddd;font:10px;padding: 5px 15px 5px 15px;}");
        layout_menu->addWidget(button);
        button_list->push_back(button);
    }

    QObject::connect((*button_list)[0],&QPushButton::clicked,(*button_list)[0],[&](){
        show_user();
    }); //设置监听

    QObject::connect((*button_list)[1],&QPushButton::clicked,(*button_list)[1],[&](){
        show_subscribed();
    }); //设置监听

    QObject::connect((*button_list)[2],&QPushButton::clicked,(*button_list)[2],[&](){
        book_file_op(1);
    }); //设置监听

    if(user->get_authority() == 0)
    {
        QString admin_label[] = {"导入", "添加", "成员"};
        for(int i = 0; i < 3; i++)
        {
            QPushButton *button = new QPushButton;
            button->setText(admin_label[i]);
            button->setStyleSheet("QLabel{background:#dddddd;font:10px;padding: 5px 15px 5px 15px;}");
            layout_menu->addWidget(button);
            button_list->push_back(button);
        }
        QObject::connect((*button_list)[3],&QPushButton::clicked,(*button_list)[3],[&](){
            book_file_op(0);
        }); //设置监听
        QObject::connect((*button_list)[4],&QPushButton::clicked,(*button_list)[4],[&](){
            show_add();
        }); //设置监听
        QObject::connect((*button_list)[5],&QPushButton::clicked,(*button_list)[5],[&](){
            show_member();
        }); //设置监听

    }

    button_help->setText("帮助");
    button_help->setStyleSheet("QLabel{background:#dddddd;font:10px;padding: 5px 15px 5px 15px;}");
    layout_menu->addWidget(button_help);
    QObject::connect(button_help,&QPushButton::clicked,button_help,[&](){
        show_help();
    }); //设置监听

    layout_menu->addStretch(3);
}

void MainWindow::init_view_func()
{
    layout_search->setDirection(QBoxLayout::LeftToRight);//设置排列方式
    layout_search->setSpacing(10);    //设置控件距离
    layout_func->addLayout(layout_search);
    init_view_search();

    layout_table->setDirection(QBoxLayout::TopToBottom); //设置排列方式
    layout_table->setSpacing(10);    //设置控件距离
    layout_func->addLayout(layout_table);
    init_view_table();
}

void MainWindow::init_view_search()
{
    input_search->setPlaceholderText("请输入关键字");

    input_search->setStyleSheet("QLabel{font:10px;padding: 0px 5px 0px 5px;}");
    input_search->setClearButtonEnabled(true);
    layout_search->addWidget(input_search,9);

    but_search->setText("搜索");
    but_search->setToolTip("仅支持完整书名搜索");
    QObject::connect(but_search,&QPushButton::clicked,but_search,[&](){
        QString key = input_search->text();
        QString condition = key.isEmpty() ? NULL : "name='" + key + "'";
        reflash_tabel(condition);
    }); //设置监听
    layout_search->addWidget(but_search,1);
}

void MainWindow::init_view_table()
{
    for(int i = 0; i < 3; i++)
    {
        QLabel *label = new QLabel();
        label_list->push_back(label);
    }

    layout_column->addStretch(1);
    (*label_list)[0]->setText("书名");
    (*label_list)[0]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[0], 18);
    (*label_list)[1]->setText("作者");
    (*label_list)[1]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[1], 12);
    (*label_list)[2]->setText("状态");
    (*label_list)[2]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[2], 6);
    layout_table->addLayout(layout_column);

    layout_table->addWidget(list_books);
    reflash_tabel(NULL);
    QObject::connect(list_books,&QListWidget::itemClicked,list_books,[&]{
        int row = list_books->currentRow(); // 选中的item
        if((*list_state)[row] == "0")
        {
            book_op((*list_id)[row]);
        }
    });
}

void MainWindow::show()
{
    widget->show();
}

void MainWindow::reflash_tabel(QString condition)
{
    SQLHelper sql_helper;
    sql_helper.connect();

    QString sql = "SELECT books.id,books.name,books.author,subscribe.id FROM books LEFT JOIN subscribe ON books.id = subscribe.book_id";
    if(condition.isNull())
    {
        sql += ";";
    }
    else
    {
        sql += " where " + condition + ";";
    }

    QSqlQuery* query = sql_helper.exec_sql(sql);

    if(query == NULL)
    {
        std::cout << "selete books failed!" << std::endl;
        return;
    }

    list_id->clear();
    list_name->clear();
    list_author->clear();
    list_state->clear();

    int count = 0;

    while (query->next()) {
        if(count == 100)    // 只显示100条
        {
            break;
        }
        list_id->push_back(query->value(0).toString());
        list_name->push_back(query->value(1).toString());
        list_author->push_back(query->value(2).toString());
        list_state->push_back(query->value(3).toString());
        count++;
    }


    list_books->clear();
    for(int i = 0; i < count; i++)
    {
        QListWidgetItem *item=new QListWidgetItem(list_books);  // item
        QWidget *widget = new QWidget(list_books);  // item视图
        item->setSizeHint(QSize(120, 60));  // item大小
        QHBoxLayout *layout_item = new QHBoxLayout(widget); // item布局

        QLabel *label_book_name = new QLabel(widget);   // 书名label
        label_book_name->setText((*list_name)[i]);
        label_book_name->setStyleSheet("QLabel{font:10px;}");

        QLabel *label_book_author = new QLabel(widget); // 作者label
        label_book_author->setText((*list_author)[i]);
        label_book_author->setStyleSheet("QLabel{font:10px;}");

        QLabel *label_book_statu = new QLabel(widget); // 状态label
        if((*list_state)[i] == "0")
        {
            label_book_statu->setText("未被订阅");
        }
        else
        {
            label_book_statu->setText("已被订阅");
        }
        label_book_author->setStyleSheet("QLabel{font:10px;}");

        layout_item->addWidget(label_book_name, 9);
        layout_item->addWidget(label_book_author, 6);
        layout_item->addWidget(label_book_statu, 3);

        widget->setLayout(layout_item);

        list_books->setItemWidget(item,widget);
    }

    list_books->update();

    std::cout << "tabel reflash!" << std::endl;
}

void MainWindow::show_user()
{
    user_dialog = new UserDialog(user);
    user_dialog->init_view_main();
    user_dialog->show();
}

void MainWindow::show_help()
{
    help_dialog = new HelpDialog();
    help_dialog->init_view_main();
    help_dialog->show();
}

void MainWindow::show_add()
{
    add_dialog = new AddDialog();
    add_dialog->init_view_main();
    add_dialog->show();
    QObject::connect(add_dialog,&AddDialog::destroyed,add_dialog,[&]{
        reflash_tabel(NULL);
    });
    QObject::connect(add_dialog,&AddDialog::finished,add_dialog,[&]{
        reflash_tabel(NULL);
    });
}

void MainWindow::show_subscribed()
{
    subscribed_dialog = new SubscribedDialog(*user);
    subscribed_dialog->init_view_main();
    subscribed_dialog->show();
    QObject::connect(subscribed_dialog,&SubscribedDialog::destroyed,subscribed_dialog,[&]{
        reflash_tabel(NULL);
    });
    QObject::connect(subscribed_dialog,&SubscribedDialog::finished,subscribed_dialog,[&]{
        reflash_tabel(NULL);
    });
}

void MainWindow::book_op(QString book_id)
{
    book_op_dialog = new BookOpDialog(*user, book_id);
    book_op_dialog->init_view_main();
    book_op_dialog->show();
    QObject::connect(book_op_dialog,&BookOpDialog::destroyed,book_op_dialog,[&]{
        reflash_tabel(NULL);
    });
    QObject::connect(book_op_dialog,&BookOpDialog::finished,book_op_dialog,[&]{
        reflash_tabel(NULL);
    });
}

void MainWindow::book_file_op(int op)
{
    book_file_dialog = new BookFileDialog(op);
    book_file_dialog->init_view_main();
    book_file_dialog->show();
    QObject::connect(book_file_dialog,&BookFileDialog::destroyed,book_file_dialog,[&]{
        reflash_tabel(NULL);
    });
    QObject::connect(book_file_dialog,&BookFileDialog::finished,book_file_dialog,[&]{
        reflash_tabel(NULL);
    });
}

void MainWindow::show_member()
{
    member_dialog = new MemberDialog();
    member_dialog->init_view_main();
    member_dialog->show();
}
